<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class NoticeController extends Controller
{
    /**
   *    公告的添加页面
   */
   public function getAdd(Request $request)
   {

        return view('/admin/notice/add');
   }
   /**
   *	公告的添加方法
   */
   public function postInsert(Request $request)
   {
   	//判断提交的数据
   	 $this->validate($request, [
   	 	'title'=>'required',
   	 	'path'=>'required',
   	 	],[
   	 	'title.required'=>'标题不能为空',
   	 	'path.required'=>'跳转页面不能为空',
   	 	]);
   	 $res = $request->except('_token');

   	 $pro = DB::table('ug_notice')->insert($res);
   	 if($pro){
   	 	 return redirect('/admin/notice/index')->with('success','添加成功');
   	 }else{
   	 	return back()->with('error','添加失败');
   	 }
   }

   /**
   *	公告的显示页面
   */
   public function getIndex(Request $request)
   {
   	$res = DB::table('ug_notice')->
   	where('title','like','%'.$request->input('search').'%')->
    paginate($request->input('num',10));

   	return view('/admin/notice/index',['row'=>$res,'request'=>$request->all()]);
   }
   /**
   *	公告的编辑页面
   */
   public function getEdit(Request $request)
   {
   	$id = $request->input('id');
   	$res = DB::table('ug_notice')->where('id',$id)->first();

   	return view('/admin/notice/edit',['res'=>$res]);
   }
   /**
   *	公告的修改方法
   */
   public function postUpdate(Request $request)
   {
   	 $res = $request->except('_token');
   	 $id = $request->input('id');
   	 $db = DB::table('ug_notice')->where('id',$id)->update($res);
   	 if($db){
   	 	return redirect('/admin/notice/index')->with('success','修改成功');
   	 }else{
   	 	return back()->with('error','修改失败');
   	 }
   }
   /**
   *	公告的删除方法
   */
   public function getDelete(Request $request)
   {
   	$id = $request->input('id');
   	$res = DB::table('ug_notice')->where('id',$id)->delete();
   	if($res){
   		return redirect('/admin/notice/index')->with('success','删除成功');
   	}else{
   		return back()->with('error','删除失败');
   	}
   }
   /**
   *	公告的前台显示
   */
   public function getShow(Request $request)
   {
   		$res = DB::table('ug_notice')->where('status','=',1)->get();
   		$row='';
   		foreach($res as $k=>$v)
   		{
   			$row.='<li >
              <a target="_blank" href="'.$v->path.'">'.$v->title.'</a>
            </li>';
   		}

   		return $row;
   }
}
